package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.pojo.*;
import com.qf.service.ProductCommentsService;
import com.qf.service.ProductParamsService;
import com.qf.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 87380
 * @date 2021/12/3
 */
@RestController
@RequestMapping("/product")
@CrossOrigin("http://localhost:8081/")
public class ProductDetailController {

    @Autowired
    public ProductSkuService productSkuService;
    @Autowired
    public ProductParamsService productParamsService;
    @Autowired
    public ProductCommentsService productCommentsService;

    @GetMapping("/sku/{id}")
    public ResultData<ProductSku> getSku(@PathVariable Integer id){
        ProductSku productSku = productSkuService.skuById(id);
        if (productSku == null){
            return new ResultData<>(10000,"没有此商品！");
        }
        return new ResultData<>(0,"查询成功",productSku);
    }

    @GetMapping("/params/{id}")
    public ResultData<ProductParams> getParams(@PathVariable Integer id){
        ProductParams params = productParamsService.paramsById(id);
        if (params == null){
            return new ResultData<>(10000,"没有此商品！");
        }
        return new ResultData<>(0,"查询成功",params);
    }

    @GetMapping("/comments/{id}")
    public ResultData<Page<ProductComments>> getComments(@PathVariable Integer id,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        Page<ProductComments> comments = productCommentsService.commentsById(id,pageNum,pageSize);
        if (comments.getRecords().isEmpty()){
            return new ResultData<>(10000,"暂无评论哦！");
        }
        return new ResultData<>(0, "查询成功", comments);
    }



}
